package com.ffz.calcitesql.parser;

import com.ffz.calcitesql.enums.EngineType;
import org.apache.calcite.avatica.util.Casing;
import org.apache.calcite.config.NullCollation;
import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlWriterConfig;
import org.apache.calcite.sql.dialect.*;
import org.apache.calcite.sql.pretty.SqlPrettyWriter;
import org.apache.calcite.sql.validate.SqlConformanceEnum;

import java.util.Arrays;
import java.util.List;

public class CommonSqlParser {
    private List<EngineType> typeList = Arrays.asList(EngineType.PRESTO, EngineType.HIVE, EngineType.SPARK);
    private final SqlWriterConfig SQL_WRITER_CONFIG = SqlPrettyWriter.config()
            .withAlwaysUseParentheses(true)
            .withUpdateSetListNewline(false)
            .withFromFolding(SqlWriterConfig.LineFolding.TALL)
            .withIndentation(0);

    // 根据引擎类型获取不同引擎的解析配置
    private SqlDialect getDefaultSqlDialect(EngineType engineType) {
        switch (engineType) {
            case SPARK:
                return SparkSqlDialect.DEFAULT;
            case PRESTO:
                return PrestoSqlDialect.DEFAULT;
            case TEZ:
            case HIVE:
                SqlDialect.Context context = SqlDialect.EMPTY_CONTEXT.withDatabaseProduct(SqlDialect.DatabaseProduct.HIVE)
                        .withQuotedCasing(Casing.UNCHANGED)
                        .withUnquotedCasing(Casing.UNCHANGED)
                        .withNullCollation(NullCollation.LOW)
                        .withIdentifierQuoteString("`")
                        .withCaseSensitive(false);
                return new HiveSqlDialect(context);
            default:
                return CalciteSqlDialect.DEFAULT;
        }
    }
    // 根据引擎类型获取不同引擎的输出SQL配置
    private SqlConformanceEnum getDefaultSqlConformanceEnum(EngineType engineType){
        switch (engineType){
            case PRESTO:
                return SqlConformanceEnum.PRESTO;
            case SPARK:
            case TEZ:
            case HIVE:
                return SqlConformanceEnum.DEFAULT;
            default:
                return SqlConformanceEnum.DEFAULT;
        }
    }
    public SqlNode parseSql(String sql,EngineType engineType){
         if(EngineType.AUTO.equals(engineType)){
             for (EngineType engineType1:typeList){

             }
         }else{

         }
         throw new RuntimeException("解析SQL错误");
    }
}
